package mysql

import (
	"data-transfer/domain"
	"fmt"
	"sync/atomic"
)
import "context"

// InsertEventRemind 新增财公司大事提醒
func (mdb *db) InsertEventRemind(ctx context.Context, rows []*domain.EVENT_REMIND) (int64, error) {
	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 50, execFun)

	return affected, err
}

//InsertEventRemindSingle 新增公司大事提醒，用于goCanal监控
func (mdb *db) InsertEventRemindSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["DECLAREDATE"], insertedMap["EVENTTEXT"], insertedMap["SOURCETABLE"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["EVENTTITLE"], insertedMap["RANK"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateEventRemind 更新公司大事提醒，用于goCanal监控
func (mdb *db) UpdateEventRemind(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["EVENTTEXT"], updateMap["SOURCETABLE"], updateMap["COMPCODE"], updateMap["DECLAREDATE"], updateMap["EVENTTITLE"], updateMap["RANK"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteEventRemind 删除公司大事提醒，用于goCanal监控
func (mdb *db) DeleteEventRemind(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["DECLAREDATE"], deleteMap["EVENTTITLE"], deleteMap["RANK"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertDividend 新增分红配送
func (mdb *db) InsertDividend(ctx context.Context, rows []*domain.DIVIDEND) (int64, error) {
	defVal := "0.000000"
	//判断decimal数值情况
	for _, v := range rows {
		if v.Cdividend == "" {
			v.Cdividend = defVal
		}
	}

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertDividendSingle 新增分红配送，用于用于goCanal监控
func (mdb *db) InsertDividendSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["BONUSIMPDATE"], insertedMap["EXRIGHTDATE"], insertedMap["CDIVIDEND"], insertedMap["BONUSYEAR"], insertedMap["DIVIDTYPE"], insertedMap["DIVITYPE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateDividend 修改分红配送，用于用于goCanal监控
func (mdb *db) UpdateDividend(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["BONUSIMPDATE"], updateMap["CDIVIDEND"], updateMap["COMPCODE"], updateMap["EXRIGHTDATE"], updateMap["BONUSYEAR"], updateMap["DIVIDTYPE"], updateMap["DIVITYPE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteDividend 删除分红配送，用于用于goCanal监控
func (mdb *db) DeleteDividend(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["EXRIGHTDATE"], deleteMap["BONUSYEAR"], deleteMap["DIVIDTYPE"], deleteMap["DIVITYPE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertMargin 新增融资融券
func (mdb *db) InsertMargin(ctx context.Context, rows []*domain.MARGIN) (int64, error) {
	checkMarginDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 50, execFun)

	return affected, err
}

// InsertMarginSingle 新增融资融券，用于goCanal监控
func (mdb *db) InsertMarginSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["TRADEDATE"], insertedMap["SYMBOL"], insertedMap["COMPCODE"], insertedMap["FINBALANCE"], insertedMap["FINBUYAMT"], insertedMap["MRGGBAL"], insertedMap["MRGNRESQTY"], insertedMap["MRGNSELLAMT"], insertedMap["FINMRGHBAL"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateMargin 更改融资融券，用于goCanal监控
func (mdb *db) UpdateMargin(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["COMPCODE"], updateMap["FINBALANCE"], updateMap["FINBUYAMT"], updateMap["MRGGBAL"], updateMap["MRGNRESQTY"], updateMap["MRGNSELLAMT"], updateMap["FINMRGHBAL"], updateMap["SYMBOL"], updateMap["TRADEDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteMargin 删除融资融券，用于goCanal监控
func (mdb *db) DeleteMargin(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["SYMBOL"], deleteMap["TRADEDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// InsertProfitForecast 新增公司业绩盈利预测
func (mdb *db) InsertProfitForecast(ctx context.Context, rows []*domain.PROFIT_FORECAST) (int64, error) {
	checkProfitForcastDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

// InsertProfitForecastSingleNew 新增公司业绩盈利预测,用于goCanal监控
func (mdb *db) InsertProfitForecastSingleNew(insertSql string, insertedMap map[string]string) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["EXPTYEAR"], insertedMap["OPERMINCOME"], insertedMap["RETAMAXPROFITS"], insertedMap["EPSMAXFORE"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["PUBLISHDATE"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// UpdateProfitForecastNew2 更改公司业绩盈利预测，单纯更新时使用,用于goCanal监控
func (mdb *db) UpdateProfitForecastNew2(updateSql string, updateMap map[string]string) (int64, error) {
	// ` SET  `OPERMINCOME` = ?,`RETAMAXPROFITS` = ?,`EPSMAXFORE` = ?   WHERE `COMPCODE` = ? AND `SYMBOL` =?  AND `PUBLISHDATE` = ? AND `EXPTYEAR` =? AND ID > 0"
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["OPERMINCOME"], updateMap["RETAMAXPROFITS"], updateMap["EPSMAXFORE"], updateMap["COMPCODE"], updateMap["SYMBOL"], updateMap["PUBLISHDATE"], updateMap["EXPTYEAR"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// UpdateProfitForecastNew 更改公司业绩盈利预测,用于goCanal监控
func (mdb *db) UpdateProfitForecastNew(updateSql string, updateMap map[string]string) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["OPERMINCOME"], updateMap["RETAMAXPROFITS"], updateMap["EPSMAXFORE"], updateMap["PUBLISHDATE"], updateMap["COMPCODE"], updateMap["SYMBOL"], updateMap["EXPTYEAR"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// DeleteProfitForecastNew 删除公司业绩盈利预测,用于goCanal监控
func (mdb *db) DeleteProfitForecastNew(deleteSql string, deleteMap map[string]string) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["SYMBOL"], deleteMap["PUBLISHDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// InsertProfitForecastSingle 新增公司业绩盈利预测,用于goCanal监控
func (mdb *db) InsertProfitForecastSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["EXPTYEAR"], insertedMap["OPERMINCOME"], insertedMap["RETAMAXPROFITS"], insertedMap["EPSMAXFORE"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["PUBLISHDATE"], insertedMap["DATASOURCE"], insertedMap["SESSIONBEGDATE"], insertedMap["SESSIONENDDATE"], insertedMap["BASESSIONBEGDATE"], insertedMap["BASESSIONENDDATE"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// UpdateProfitForecast 更改公司业绩盈利预测,用于goCanal监控
func (mdb *db) UpdateProfitForecast(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["EXPTYEAR"], updateMap["OPERMINCOME"], updateMap["RETAMAXPROFITS"], updateMap["EPSMAXFORE"], updateMap["COMPCODE"], updateMap["PUBLISHDATE"], updateMap["DATASOURCE"], updateMap["SESSIONBEGDATE"], updateMap["SESSIONENDDATE"], updateMap["BASESSIONBEGDATE"], updateMap["BASESSIONENDDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// DeleteProfitForecast 删除公司业绩盈利预测,用于goCanal监控
func (mdb *db) DeleteProfitForecast(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["PUBLISHDATE"], deleteMap["DATASOURCE"], deleteMap["SESSIONBEGDATE"], deleteMap["SESSIONENDDATE"], deleteMap["BASESSIONBEGDATE"], deleteMap["BASESSIONENDDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// InsertRatingCount 新增机构评级统计数据
func (mdb *db) InsertRatingCount(ctx context.Context, rows []*domain.RATING_COUNT) (int64, error) {
	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 50, execFun)

	return affected, err
}

//UpdateRatingCount 更新机构评级统计数据，用于goCanal监控
func (mdb *db) UpdateRatingCount(updateSql string, row *domain.RATING_COUNT) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, row.Buy, row.Enlarge, row.Neutral, row.Reduce, row.Sell, row.Symbol, row.Ratingvalid)
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertRatingCountSingle 新增机构评级统计数据，用于goCanal监控
func (mdb *db) InsertRatingCountSingle(insertSql string, row *domain.RATING_COUNT) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, row.Buy, row.Enlarge, row.Neutral, row.Reduce, row.Sell, row.Ratingvalid, row.Stdrating, row.Compcode, row.Symbol, row.Exchange)
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// DeleteRatingCount 删除评级机构统计
func (mdb *db) DeleteRatingCount(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["SYMBOL"], deleteMap["RATINGVALID"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// HasRatingCount 判断是否存在某一证券的评级有效期的统计数据
func (mdb *db) HasRatingCount(symbol string, ratingValid string) (bool, error) {
	ratingCount := new(domain.RATING_COUNT)
	has, err := mdb.db.NewSession().Where("SYMBOL=? and RATINGVALID = ?  ", symbol, ratingValid).Get(ratingCount)
	return has, err
}

//InsertRating 插入机构评级信息
func (mdb *db) InsertRating(ctx context.Context, rows []*domain.RATING) (int64, error) {
	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 60, execFun)

	return affected, err
}

//GetLastRating 取得上次评级（根据ID主键递增性获得），用于goCanal监控
func (mdb *db) GetLastRating(skcode string, reportdate string, ratingValid string, analystName string) (string, error) {
	var results []*domain.RATING
	err := mdb.db.NewSession().Where("SKCODE = ? and REPORTDATE = ? and RATINGVALID = ? and ANALYSTNAME = ?", skcode, reportdate, ratingValid, analystName).Desc("ID").Find(&results)
	if err != nil {
		return "", err
	}
	if len(results) > 0 {
		return results[0].Rating, nil //ID最新的最新评级作为上次评级
	}
	return "", nil
}

// UpdateRating 更改机构评级信息,用于goCanal监控
func (mdb *db) UpdateRating(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["COMPNAME"], updateMap["RATING"], updateMap["LASTRATING"], updateMap["RADJUSTDIR"], updateMap["EXCHANGE"], updateMap["SKCODE"], updateMap["REPORTDATE"], updateMap["RATINGVALID"], updateMap["ANALYSTNAME"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//InsertRatingSingle 新增机构评级信息，用于goCanal监控
func (mdb *db) InsertRatingSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["SKCODE"], insertedMap["REPORTDATE"], insertedMap["COMPNAME"], insertedMap["RATING"], insertedMap["LASTRATING"], insertedMap["RADJUSTDIR"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["RATINGVALID"], insertedMap["ANALYSTNAME"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//DeleteRating 删除机构评级信息，用于goCanal监控
func (mdb *db) DeleteRating(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["SKCODE"], deleteMap["REPORTDATE"], deleteMap["RATINGVALID"], deleteMap["ANALYSTNAME"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertEquityStructure 插入股本结构
func (mdb *db) InsertEquityStructure(ctx context.Context, rows []*domain.EQUITY_STRUCTURE) (int64, error) {
	checkEquityStructureDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertEquityStructureSingle 插入股本结构 用于goCanal 监控
func (mdb *db) InsertEquityStructureSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {

	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["TOTALSHARE"], insertedMap["CIRCSKAMT"], insertedMap["CIRCSKRTO"],
		insertedMap["CIRCSKAMTNOLIM"], insertedMap["CIRCSKRTONOLIM"], insertedMap["CIRCHAMT"], insertedMap["HSHARERTO"], insertedMap["NCIRCAMT"],
		insertedMap["NONNEGSKRTO"], insertedMap["CIRCAAMT"], insertedMap["ASHARERTO"], insertedMap["CIRCBAMT"], insertedMap["BSHARERTO"], insertedMap["NCIRCAMT1"], insertedMap["NONNEGSKRTO1"], insertedMap["BEGINDATE"], insertedMap["EXRIGHTDATE"], insertedMap["PUBLISHDATE"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//UpdateEquityStructure  更改股本结构，用于goCanal监控
func (mdb *db) UpdateEquityStructure(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["TOTALSHARE"], updateMap["CIRCSKAMT"], updateMap["CIRCSKRTO"], updateMap["CIRCSKAMTNOLIM"], updateMap["CIRCSKRTONOLIM"], updateMap["CIRCHAMT"], updateMap["HSHARERTO"], updateMap["NCIRCAMT"], updateMap["NONNEGSKRTO"], updateMap["CIRCAAMT"], updateMap["ASHARERTO"], updateMap["CIRCBAMT"], updateMap["BSHARERTO"], updateMap["NCIRCAMT1"], updateMap["NONNEGSKRTO1"], updateMap["PUBLISHDATE"], updateMap["BEGINDATE"], updateMap["COMPCODE"], updateMap["EXRIGHTDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteEquityStructure  删除股本结构，用于goCanal监控
func (mdb *db) DeleteEquityStructure(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	// "DELETE FROM `EQUITY_STRUCTURE` WHERE WHERE `BEGINDATE` = ? AND `COMPCODE` = ? AND `EXRIGHTDATE` = ?"
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["BEGINDATE"], deleteMap["COMPCODE"], deleteMap["EXRIGHTDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertEquityStruChgSingle 插入股本变动，用于goCanal监控
func (mdb *db) InsertEquityStruChgSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["BEGINDATE"], insertedMap["SHCHGRSN"], insertedMap["TOTALSHARE"], insertedMap["TOTALSHARECHG"], insertedMap["SKCHGEXP"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["EXRIGHTDATE"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdatetEquityStruChg  更改股本变动，用于goCanal监控
func (mdb *db) UpdatetEquityStruChg(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["SHCHGRSN"], updateMap["TOTALSHARE"], updateMap["TOTALSHARECHG"], updateMap["SKCHGEXP"], updateMap["BEGINDATE"], updateMap["COMPCODE"], updateMap["EXRIGHTDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeletetEquityStruChg  删除股本变动，用于goCanal监控
func (mdb *db) DeletetEquityStruChg(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["BEGINDATE"], deleteMap["COMPCODE"], deleteMap["EXRIGHTDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// InsertEquityStruChg 插入股本变动
func (mdb *db) InsertEquityStruChg(ctx context.Context, rows []*domain.EQUITY_STRUCTURE_CHANGE) (int64, error) {
	checkEquityStruChgDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertTenCirculatingSharesHolder 插入十大流通股股东信息
func (mdb *db) InsertTenCirculatingSharesHolder(ctx context.Context, rows []*domain.TEN_CIRCULATING_SHARES_HOLDER) (int64, error) {
	checkTenCirculatingSharesHolderDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertTenCirculatingSharesHolderSingle 插入十大流通股股东，用于goCanal监控
func (mdb *db) InsertTenCirculatingSharesHolderSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["SHHOLDERNAME"], insertedMap["HOLDERRTO"], insertedMap["PCTOFFLOTSHARES"], insertedMap["HOLDERSUMCHG"], insertedMap["HOLDERSUMCHGRATE"], insertedMap["SHHOLDERCODE"], insertedMap["HOLDERAMT"], insertedMap["PUBLISHDATE"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["ENDDATE"], insertedMap["RANK"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//UpdateTenCirculatingSharesHolder 更改十大流通股股东，用于goCanal监控
func (mdb *db) UpdateTenCirculatingSharesHolder(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["HOLDERRTO"], updateMap["PCTOFFLOTSHARES"], updateMap["HOLDERSUMCHG"], updateMap["HOLDERSUMCHGRATE"], updateMap["SHHOLDERCODE"], updateMap["HOLDERAMT"], updateMap["PUBLISHDATE"], updateMap["COMPCODE"], updateMap["SHHOLDERNAME"], updateMap["ENDDATE"], updateMap["RANK"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//DeleteTenCirculatingSharesHolder 删除十大流通股东，用于goCanal监控
func (mdb *db) DeleteTenCirculatingSharesHolder(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["SHHOLDERNAME"], deleteMap["ENDDATE"], deleteMap["RANK"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertBonusFinancing 插入分红融资
func (mdb *db) InsertBonusFinancing(ctx context.Context, rows []*domain.BONUS_FINANCING) (int64, error) {
	checkBonusFinancingDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertBonusFinancingSingle 插入分红融资，用于goCanal监控
func (mdb *db) InsertBonusFinancingSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["RECORDDATE"], insertedMap["EXRIGHTDATE"], insertedMap["DIVIBEGDATE"], insertedMap["TAXCDIVIDEND"], insertedMap["BONUSIMPDATE"],
		insertedMap["CAPSTKBASDATE"], insertedMap["DIVIIMPMARKD"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["BONUSYEAR"], insertedMap["DIVIDTYPE"], insertedMap["DIVITYPE"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateBonusFinancing 更新分红融资，用于goCanal监控
func (mdb *db) UpdateBonusFinancing(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["RECORDDATE"], updateMap["DIVIBEGDATE"], updateMap["TAXCDIVIDEND"], updateMap["BONUSIMPDATE"], updateMap["CAPSTKBASDATE"], updateMap["DIVIIMPMARKD"], updateMap["COMPCODE"], updateMap["BONUSYEAR"], updateMap["DIVIDTYPE"], updateMap["DIVITYPE"], updateMap["EXRIGHTDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteBonusFinancing 删除分红融资，用于goCanal监控
func (mdb *db) DeleteBonusFinancing(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["BONUSYEAR"], deleteMap["DIVIDTYPE"], deleteMap["DIVITYPE"], deleteMap["EXRIGHTDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertRankList 插入龙虎榜信息
func (mdb *db) InsertRankList(ctx context.Context, rows []*domain.RANK_LIST) (int64, error) {
	checkRankListDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 30, execFun)

	return affected, err
}

//InsertRankListSingle 插入龙虎榜信息，用于goCanal监控
func (mdb *db) InsertRankListSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["SYMBOL"], insertedMap["COMPCODE"], insertedMap["TRADEDATE"], insertedMap["BIZSUNITNAME"], insertedMap["AMOUNT"], insertedMap["BUYAMT"], insertedMap["SALEAMT"], insertedMap["CHGTYPE"], insertedMap["BIZSUNITCODE"], insertedMap["TRANSDIR"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateRankList 更改龙虎榜信息，用于goCanal监控
func (mdb *db) UpdateRankList(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["COMPCODE"], updateMap["BIZSUNITNAME"], updateMap["AMOUNT"], updateMap["BUYAMT"], updateMap["SALEAMT"], updateMap["EXCHANGE"], updateMap["SYMBOL"], updateMap["TRADEDATE"], updateMap["CHGTYPE"], updateMap["BIZSUNITCODE"], updateMap["TRANSDIR"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteRankList 删除龙虎榜信息，用于goCanal监控
func (mdb *db) DeleteRankList(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["SYMBOL"], deleteMap["TRADEDATE"], deleteMap["CHGTYPE"], deleteMap["BIZSUNITCODE"], deleteMap["TRANSDIR"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertBlockTrading  插入大宗交易
func (mdb *db) InsertBlockTrading(ctx context.Context, rows []*domain.BLOCK_TRADING) (int64, error) {
	checkBlockTradingDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertBlockTradingSingle 插入大宗交易，用于goCanal监控
func (mdb *db) InsertBlockTradingSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["SYMBOL"], insertedMap["COMPCODE"], insertedMap["TRADEDATE"], insertedMap["TRADEPRICE"], insertedMap["VOLUME"], insertedMap["AMOUNT"], insertedMap["BUYERNAME"], insertedMap["SELLERNAME"], insertedMap["RATE"], insertedMap["BLOCKTRADEID"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateBlockTrading 更改大宗交易，用于GoCanal监控
func (mdb *db) UpdateBlockTrading(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["COMPCODE"], updateMap["TRADEDATE"], updateMap["TRADEPRICE"], updateMap["VOLUME"], updateMap["AMOUNT"], updateMap["BUYERNAME"], updateMap["SELLERNAME"], updateMap["RATE"], updateMap["SYMBOL"], updateMap["BLOCKTRADEID"], updateMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteBlockTrading 删除大宗交易，用于goCanal监控
func (mdb *db) DeleteBlockTrading(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["SYMBOL"], deleteMap["BLOCKTRADEID"], deleteMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertCompanyExecutives 插入公司高管信息
func (mdb *db) InsertCompanyExecutives(ctx context.Context, rows []*domain.COMPANY_EXECUTIVES) (int64, error) {
	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 10, execFun)

	return affected, err
}

//UpdateCompanyExecutives 更新公司高管信息
func (mdb *db) UpdateCompanyExecutives(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["CNAME"], updateMap["GENDER"], updateMap["BIRTHDAY"], updateMap["DEGREE"], updateMap["MEMO"], updateMap["POSTYPE"], updateMap["NOWSTATUS"], updateMap["COMPCODE"], updateMap["PERSONALCODE"], updateMap["ACTDUTYNAME"], updateMap["BEGINDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertCompanyExecutivesSingle 新增公司高管信息，用于goCanal监控
func (mdb *db) InsertCompanyExecutivesSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["SYMBOL"], insertedMap["COMPCODE"], insertedMap["CNAME"], insertedMap["ACTDUTYNAME"], insertedMap["BEGINDATE"], insertedMap["GENDER"], insertedMap["BIRTHDAY"], insertedMap["DEGREE"], insertedMap["MEMO"], insertedMap["PERSONALCODE"], insertedMap["POSTYPE"], insertedMap["NOWSTATUS"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//DeleteCompanyExecutives 删除公司高管信息，用于goCanal监控
func (mdb *db) DeleteCompanyExecutives(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["PERSONALCODE"], deleteMap["ACTDUTYNAME"], deleteMap["BEGINDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// InsertMainComposition 插入公司主要业务
func (mdb *db) InsertMainComposition(ctx context.Context, rows []*domain.MAIN_COMPOSITION) (int64, error) {
	checkMainCompositionDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertMainCompositionSingle 插入公司主要业务，用于goCanal监控
func (mdb *db) InsertMainCompositionSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["PRODUCTINFO"], insertedMap["BIZTOTCOST"], insertedMap["REPORTYEAR"], insertedMap["PERPROFIT"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["PUBLISHDATE"], insertedMap["TYPESTYLE"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateMainComposition 更新公司主要业务，用于goCanal 监控
func (mdb *db) UpdateMainComposition(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["PRODUCTINFO"], updateMap["BIZTOTCOST"], updateMap["REPORTYEAR"], updateMap["PERPROFIT"], updateMap["COMPCODE"], updateMap["PUBLISHDATE"], updateMap["TYPESTYLE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//DeleteMainComposition 删除公司主要业务，用于GoCanal监控
func (mdb *db) DeleteMainComposition(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["PUBLISHDATE"], deleteMap["TYPESTYLE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertCompanyProfile 插入公司基本情况信息
func (mdb *db) InsertCompanyProfile(ctx context.Context, rows []*domain.COMPANY_PROFILE) (int64, error) {
	defVal := "0.000000"
	//判断decimal数值情况
	for _, v := range rows {
		if v.Regcapital == "" {
			v.Regcapital = defVal
		}
	}

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err

}

//InsertCompanyProfileSingle 插入公司基本信息，用于goCanal监控
func (mdb *db) InsertCompanyProfileSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {

	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["COMPNAME"], insertedMap["REGNAME"], insertedMap["COMPTYPE"], insertedMap["LISTDATE"], insertedMap["LEGREP"], insertedMap["REGCAPITAL"], insertedMap["REGADDR"], insertedMap["MAJORBIZ"], insertedMap["BIZSCOPE"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateCompanyProfile 更新公司基本信息，用于goCanal监控
func (mdb *db) UpdateCompanyProfile(updateSql string, updateMap map[string]interface{}) (int64, error) {

	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["COMPNAME"], updateMap["REGNAME"], updateMap["COMPTYPE"], updateMap["LISTDATE"], updateMap["LEGREP"], updateMap["REGCAPITAL"], updateMap["REGADDR"], updateMap["MAJORBIZ"], updateMap["BIZSCOPE"], updateMap["COMPCODE"], updateMap["SYMBOL"], updateMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//DeleteCompanyProfile 删除公司基本信息，用于goCanal监控
func (mdb *db) DeleteCompanyProfile(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["SYMBOL"], deleteMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertHolderChange 插入股东变动情况
func (mdb *db) InsertHolderChange(ctx context.Context, rows []*domain.HOLDER_CHANGE) (int64, error) {
	CheckHolderChangeDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 10, execFun)

	return affected, err
}

//InsertHolderChangeSingle 插入股东变动情况，用于goCanal监控
func (mdb *db) InsertHolderChangeSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {

	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["ENDDATE"], insertedMap["TCLOSE"], insertedMap["TOTALSHAMT"], insertedMap["TOTALSHCHGRTO"], insertedMap["KAVGSH"], insertedMap["KAVGHOLDAMT"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//UpdateHolderChange 更改股东变动情况，用于goCanal监控
func (mdb *db) UpdateHolderChange(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["TCLOSE"], updateMap["TOTALSHAMT"], updateMap["TOTALSHCHGRTO"], updateMap["KAVGSH"], updateMap["KAVGHOLDAMT"], updateMap["COMPCODE"], updateMap["ENDDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteHolderChange 删除股东变动情况，用于goCanal监控
func (mdb *db) DeleteHolderChange(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["ENDDATE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertTenHolder 插入十大股东
func (mdb *db) InsertTenHolder(ctx context.Context, rows []*domain.TEN_HOLDER) (int64, error) {
	checkTenHolderDecimalNul(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 10, execFun)

	return affected, err
}

//InsertTenHolderSingle 插入股东变动情况，用于goCanal监控
func (mdb *db) InsertTenHolderSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["SHHOLDERNAME"], insertedMap["HOLDERAMT"], insertedMap["HOLDERRTO"], insertedMap["HOLDCIRCSKAMTRTO"], insertedMap["CURCHG"], insertedMap["SHHOLDERCODE"], insertedMap["PUBLISHDATE"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["CURCHGRATE"], insertedMap["ENDDATE"], insertedMap["RANK"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//UpdateTenHolder 更改股东变动情况，用于goCanal监控
func (mdb *db) UpdateTenHolder(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["HOLDERAMT"], updateMap["HOLDERRTO"], updateMap["HOLDCIRCSKAMTRTO"], updateMap["CURCHG"], updateMap["SHHOLDERCODE"], updateMap["PUBLISHDATE"], updateMap["CURCHGRATE"], updateMap["COMPCODE"], updateMap["SHHOLDERNAME"], updateMap["ENDDATE"], updateMap["RANK"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteTenHolder 删除股东变动情况，用于goCanal监控
func (mdb *db) DeleteTenHolder(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["SHHOLDERNAME"], deleteMap["ENDDATE"], deleteMap["RANK"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertLatestIndicator 插入最新指标
func (mdb *db) InsertLatestIndicator(ctx context.Context, rows []*domain.LATEST_INDICATOR) (int64, error) {
	checkLatestIndicator(rows)

	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

//InsertLatestIndicatorSingle 插入最新指标，用于goCanal监控
func (mdb *db) InsertLatestIndicatorSingle(insertSql string, insertedMap map[string]string) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["REPORTYEAR"], insertedMap["REPORTDATETYPE"], insertedMap["BASICEPS"], insertedMap["EPSBASICEPSCUT"], insertedMap["NAPS"],
		insertedMap["OPERCFPS"], insertedMap["ROE"], insertedMap["SGPMARGIN"], insertedMap["BIZINCO"], insertedMap["OPERINYOYB"],
		insertedMap["NETPROFIT"], insertedMap["NETINPNRPLYOYB"], insertedMap["NPCUT"], insertedMap["NPGRT"], insertedMap["TOTALSHARE"],
		insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err

}

//UpdateLatestIndicator 更高最新指标，用于goCanal监控
func (mdb *db) UpdateLatestIndicator(updateMap map[string]string) (int64, error) {
	defaultVal := "0.000000" //数值型默认值

	//股票基本面简信息
	basicEps := ""   //每股收益
	naps := ""       //每股净资产
	totalShare := "" //总股本
	roe := ""        //净资产收益率
	operCfps := ""   //每股经营现金流
	npgrt := ""      //净利润同比增长

	//主要财务指标信息
	reportYear := ""
	reportDateType := ""
	epsbasicepscut := "" //扣除非经常性损益后的基本每股收益

	//一般企业利润表信息
	bizinco := ""   //营业总收入
	netprofit := "" //净利润

	//衍生财务指标信息
	sgpmargin := "" //销售毛利率
	npcut := ""     //扣除非经常性损益后的净利润

	//衍生财务指标副表
	operinyoyb := ""     ///营收同比增
	netinpnrplyoyb := "" //扣非净利润同比增

	//通用参数
	compCode, _ := updateMap["COMPCODE"]

	updateSql := "UPDATE LATEST_INDICATOR SET COMPCODE = " + fmt.Sprintf("%v", compCode) //compCode肯定是有，确保update语句正确

	//-----------------------------股票基本面简信息-------------------------------
	//以下map中的Key,确保是要存在的，如果不存在，则赋值为空

	if basicEpsI, ok := updateMap["BASICEPS"]; ok {
		if basicEpsI == "" {
			basicEps = defaultVal
		} else {
			basicEps = basicEpsI
		}
	}

	if napsI, ok := updateMap["NAPS"]; ok {
		if napsI == "" {
			naps = defaultVal
		} else {
			naps = naps
		}
	}

	if totalShareI, ok := updateMap["TOTALSHARE"]; ok {
		if totalShareI == "" {
			totalShare = defaultVal
		} else {
			totalShare = totalShareI
		}
	}

	if roeI, ok := updateMap["ROE"]; ok {
		if roeI == "" {
			roe = defaultVal
		} else {
			roe = roeI
		}
	}

	if operCfpsI, ok := updateMap["OPERCFPS"]; ok {
		if operCfpsI == "" {
			operCfps = defaultVal
		} else {
			operCfps = operCfpsI
		}
	}

	if npgrtI, ok := updateMap["NPGRT"]; ok {
		if npgrtI == "" {
			npgrt = defaultVal
		} else {
			npgrt = npgrtI
		}
	}

	if len(basicEps) > 0 {
		updateSql += ",BASICEPS=" + basicEps
	}

	if len(naps) > 0 {
		updateSql += ",NAPS=" + naps
	}

	if len(totalShare) > 0 {
		updateSql += ",TOTALSHARE=" + totalShare
	}

	if len(roe) > 0 {
		updateSql += ",ROE=" + roe
	}

	if len(operCfps) > 0 {
		updateSql += ",OPERCFPS=" + operCfps
	}

	if len(npgrt) > 0 {
		updateSql += ",NPGRT=" + npgrt
	}

	//-------------------------------------主要财务指标(产品表）-------------------------------

	if reportYearI, ok := updateMap["REPORTYEAR"]; ok {
		reportYear = fmt.Sprintf("%v", reportYearI)
	}

	if reportDateTypeI, ok := updateMap["REPORTDATETYPE"]; ok {
		reportDateType = fmt.Sprintf("%v", reportDateTypeI)
	}

	if epsbasicepscutI, ok := updateMap["EPSBASICEPSCUT"]; ok {
		if epsbasicepscutI == "" {
			epsbasicepscut = defaultVal
		} else {
			epsbasicepscut = epsbasicepscutI
		}
	}

	if len(epsbasicepscut) > 0 {
		updateSql += ",EPSBASICEPSCUT=" + epsbasicepscut
	}

	//-------------------------------------一般企业利润表-------------------------------
	if bizincoI, ok := updateMap["BIZINCO"]; ok {
		if bizincoI == "" {
			bizinco = defaultVal
		} else {
			bizinco = bizincoI
		}
	}

	if netprofitI, ok := updateMap["NETPROFIT"]; ok {
		if netprofitI == "" {
			netprofit = defaultVal
		} else {
			netprofit = netprofitI
		}
	}

	if len(bizinco) > 0 {
		updateSql += ",BIZINCO=" + bizinco
	}

	if len(netprofit) > 0 {
		updateSql += ",NETPROFIT=" + netprofit
	}

	//-------------------------------------衍生财务指标信息-------------------------------
	if sgpmarginI, ok := updateMap["SGPMARGIN"]; ok {
		if sgpmarginI == "" {
			sgpmargin = defaultVal
		} else {
			sgpmargin = sgpmarginI
		}
	}

	if npcutI, ok := updateMap["NPCUT"]; ok {
		if npcutI == "" {
			npcut = defaultVal
		} else {
			npcut = npcutI
		}
	}

	if len(sgpmargin) > 0 {
		updateSql += ",SGPMARGIN=" + sgpmargin
	}

	if len(npcut) > 0 {
		updateSql += ",NPCUT=" + npcut
	}

	//-------------------------------------衍生财务指标副表信息-------------------------------
	if operinyoybI, ok := updateMap["OPERINYOYB"]; ok {
		if operinyoybI == "" {
			operinyoyb = defaultVal
		} else {
			operinyoyb = operinyoybI
		}
	}

	if netinpnrplyoybI, ok := updateMap["NETINPNRPLYOYB"]; ok {
		if netinpnrplyoybI == "" {
			netinpnrplyoyb = defaultVal
		} else {
			netinpnrplyoyb = netinpnrplyoybI
		}
	}
	if len(operinyoyb) > 0 {
		updateSql += ", OPERINYOYB=" + operinyoyb
	}
	if len(netinpnrplyoyb) > 0 {
		updateSql += ", NETINPNRPLYOYB=" + netinpnrplyoyb
	}

	// 拼接where 通用条件
	updateSql += " WHERE COMPCODE = " + fmt.Sprintf("%v", compCode) + " AND ID > 0 " // 确保SQL语句能够通过

	if len(reportYear) > 0 {
		updateSql += " AND REPORTYEAR=" + reportYear
	}
	if len(reportDateType) > 0 {
		updateSql += " AND REPORTDATETYPE=" + reportDateType
	}

	symbol := ""

	if symbolI, ok := updateMap["SYMBOL"]; ok {
		symbol = fmt.Sprintf("%v", symbolI)
	}

	if len(symbol) > 0 {
		updateSql += " AND SYMBOL=" + symbol
	}

	var err error
	result, err := mdb.db.Exec(updateSql)
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//DeleteLatestIndicator 删除最新指标
func (mdb *db) DeleteLatestIndicator(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["SYMBOL"], deleteMap["EXCHANGE"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//HasLatestIndicator 是否含有最新指标
func (mdb *db) HasLatestIndicator(compCode string, symbol string, reportYear string, reportDateType string) (bool, error) {
	keyIndicator := new(domain.KEY_INDICATOR)
	has, err := mdb.db.NewSession().Where("SYMBOL=? and COMPCODE = ? and REPORTYEAR = ?  and REPORTDATETYPE = ? ", symbol, compCode, reportYear, reportDateType).Desc("REPORTYEAR").Get(keyIndicator)
	return has, err
}

//GetLatestPublishDateProfitForecast 取得相关symbol的最新研报日期的盈利预测数据
func (mdb *db) GetLatestPublishDateProfitForecast(symbol string) (*domain.PROFIT_FORECAST, error) {
	profitForCast := new(domain.PROFIT_FORECAST)
	_, err := mdb.db.NewSession().Where("SYMBOL = ?", symbol).Desc("PUBLISHDATE").Limit(1).Get(profitForCast)
	return profitForCast, err
}

//GetLatestPublishDateMainComposition  取得相关symbol的最新报表日期的公司经营状况数据
func (mdb *db) GetLatestPublishDateMainComposition(symbol string, compCode string) (*domain.MAIN_COMPOSITION, error) {
	mainComposition := new(domain.MAIN_COMPOSITION)
	_, err := mdb.db.NewSession().Where("SYMBOL = ? AND COMPCODE = ?", symbol, compCode).Desc("PUBLISHDATE").Limit(1).Get(mainComposition)
	return mainComposition, err
}

func checkLatestIndicator(rows []*domain.LATEST_INDICATOR) {
	defVal := "0.000000"
	for _, v := range rows {
		if v.Basiceps == "" {
			v.Basiceps = defVal
		}
		if v.Epsbasicepscut == "" {
			v.Epsbasicepscut = defVal
		}
		if v.Naps == "" {
			v.Naps = defVal
		}
		if v.Opercfps == "" {
			v.Opercfps = defVal
		}
		if v.Roe == "" {
			v.Roe = defVal
		}
		if v.Sgpmargin == "" {
			v.Sgpmargin = defVal
		}
		if v.Bizinco == "" {
			v.Bizinco = defVal
		}
		if v.Operinyoyb == "" {
			v.Operinyoyb = defVal
		}

		if v.Netprofit == "" {
			v.Netprofit = defVal
		}
		if v.Netinpnrplyoyb == "" {
			v.Netinpnrplyoyb = defVal
		}
		if v.Npcut == "" {
			v.Npcut = defVal
		}
		if v.Npgrt == "" {
			v.Npgrt = defVal
		}
		if v.Totalshare == "" {
			v.Totalshare = defVal
		}

	}
}

func checkTenHolderDecimalNul(rows []*domain.TEN_HOLDER) {
	defVal := "0.000000"
	for _, v := range rows {
		if v.Holderamt == "" {
			v.Holderamt = defVal
		}
		if v.Holderrto == "" {
			v.Holderrto = defVal
		}

		if v.Holdcircskamtrto == "" {
			v.Holdcircskamtrto = defVal
		}
		if v.Curchg == "" {
			v.Curchg = defVal
		}
		if v.Curchgrate == "" {
			v.Curchgrate = defVal
		}

	}
}
func CheckHolderChangeDecimalNul(rows []*domain.HOLDER_CHANGE) {
	defVal := "0.000000"
	for _, v := range rows {
		if v.Tclose == "" {
			v.Tclose = defVal
		}
		if v.Totalshamt == "" {
			v.Totalshamt = defVal
		}
		if v.Totalshchgrto == "" {
			v.Totalshchgrto = defVal
		}
		if v.Kavgsh == "" {
			v.Kavgsh = defVal
		}

		if v.Kavgholdamt == "" {
			v.Kavgholdamt = defVal
		}

	}
}
func checkMainCompositionDecimalNul(rows []*domain.MAIN_COMPOSITION) {
	defVal := "0.000000"
	for _, v := range rows {
		if v.Biztotcost == "" {
			v.Biztotcost = defVal
		}
		if v.Perprofit == "" {
			v.Perprofit = defVal
		}
	}
}
func checkBlockTradingDecimalNul(rows []*domain.BLOCK_TRADING) {
	defVal := "0.000000"

	for _, v := range rows {
		if v.Tradeprice == "" {
			v.Tradeprice = defVal
		}
		if v.Volume == "" {
			v.Volume = defVal
		}
		if v.Amount == "" {
			v.Amount = defVal
		}
		if v.Rate == "" {
			v.Rate = defVal
		}

	}
}
func checkRankListDecimalNul(rows []*domain.RANK_LIST) {
	defVal := "0.000000"

	for _, v := range rows {
		if v.Amount == "" {
			v.Amount = defVal
		}
		if v.Buyamt == "" {
			v.Buyamt = defVal
		}
		if v.Saleamt == "" {
			v.Saleamt = defVal
		}
	}
}
func checkBonusFinancingDecimalNul(rows []*domain.BONUS_FINANCING) {
	defVal := "0.000000"

	for _, v := range rows {
		if v.Taxcdividend == "" { //税后红利
			v.Taxcdividend = defVal
		}
	}
}
func checkTenCirculatingSharesHolderDecimalNul(rows []*domain.TEN_CIRCULATING_SHARES_HOLDER) {
	defVal := "0.000000"
	for _, v := range rows {

		if v.Holderrto == "" { //占比
			v.Holderrto = defVal
		}
		if v.Pctofflotshares == "" { //占流通股比例
			v.Pctofflotshares = defVal
		}
		if v.Holdersumchg == "" { //增减
			v.Holdersumchg = defVal
		}
		if v.Holdersumchgrate == "" { //增减幅度
			v.Holdersumchgrate = defVal
		}
		if v.Holderamt == "" { //持股数
			v.Holderamt = defVal
		}

	}
}
func checkEquityStruChgDecimalNul(rows []*domain.EQUITY_STRUCTURE_CHANGE) {
	defVal := "0.000000"
	for _, v := range rows {
		if v.Totalshare == "" {
			v.Totalshare = defVal
		}
		if v.Totalsharechg == "" {
			v.Totalsharechg = defVal
		}
	}
}
func checkProfitForcastDecimalNul(rows []*domain.PROFIT_FORECAST) {
	defVal := "0.000000"

	for _, v := range rows {
		if v.Retamaxprofits == "" {
			v.Retamaxprofits = defVal
		}
		if v.Opermincome == "" {
			v.Opermincome = defVal
		}
		if v.Epsmaxfore == "" {
			v.Epsmaxfore = defVal
		}
	}
}
func checkMarginDecimalNul(rows []*domain.MARGIN) {
	defVal := "0.000000"

	for _, v := range rows {
		if v.Finbalance == "" {
			v.Finbalance = defVal
		}

		if v.Finbuyamt == "" {
			v.Finbuyamt = defVal
		}

		if v.Mrggbal == "" {
			v.Mrggbal = defVal
		}

		if v.Mrgnresqty == "" {
			v.Mrgnresqty = defVal
		}

		if v.Mrgnsellamt == "" {
			v.Mrgnsellamt = defVal
		}
	}
}
func checkEquityStructureDecimalNul(rows []*domain.EQUITY_STRUCTURE) {
	defVal := "0.000000"

	for _, v := range rows {
		if v.Totalshare == "" {
			v.Totalshare = defVal
		}
		if v.Circskamt == "" {
			v.Circskamt = defVal
		}
		if v.Circskrto == "" {
			v.Circskrto = defVal
		}
		if v.Circskamtnolim == "" {
			v.Circskamtnolim = defVal
		}
		if v.Circskrtonolim == "" {
			v.Circskrtonolim = defVal
		}
		if v.Circhamt == "" {
			v.Circhamt = defVal
		}
		if v.Hsharerto == "" {
			v.Hsharerto = defVal
		}
		if v.Ncircamt == "" {
			v.Ncircamt = defVal
		}
		if v.Nonnegskrto == "" {
			v.Nonnegskrto = defVal
		}
		if v.Circaamt == "" {
			v.Circaamt = defVal
		}

		if v.Asharerto == "" {
			v.Asharerto = defVal
		}
		if v.Circbamt == "" {
			v.Circbamt = defVal
		}

		if v.Bsharerto == "" {
			v.Bsharerto = defVal
		}

		if v.Ncircamt1 == "" {
			v.Ncircamt1 = defVal
		}

		if v.Nonnegskrto1 == "" {
			v.Nonnegskrto1 = defVal
		}
	}
}
